package com.study.design.pattern.pattern.behavioral.observer.spring;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @Author: gongsl
 * @Date: 2025-03-23 23:47
 * @description: 事件发布者-天气站
 */
@Component
public class WeatherStation {
    private final ApplicationEventPublisher eventPublisher;
    private double currentTemperature = 25.0; // 初始温度

    public WeatherStation(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void setTemperature(double newTemperature) {
        double oldTemperature = this.currentTemperature;
        this.currentTemperature = newTemperature;
        System.out.println("WeatherStation: 温度更新 -> 旧温度: " + oldTemperature + "°C, 新温度: " + newTemperature + "°C");
        eventPublisher.publishEvent(new WeatherChangeEvent(this, oldTemperature, newTemperature));
    }
}
